package com.have.blog.admin.controller;

import com.have.blog.admin.annotation.operationLogger.OperationLogger;
import com.have.blog.admin.domain.dto.BlogTagDto;
import com.have.blog.admin.service.AdminBlogTagService;
import com.have.blog.base.exception.ThrowableUtils;
import com.have.blog.base.validator.group.Delete;
import com.have.blog.base.validator.group.GetList;
import com.have.blog.base.validator.group.Insert;
import com.have.blog.base.validator.group.Update;
import com.have.blog.base.wrapper.CommonPage;
import com.have.blog.base.wrapper.Wrapper;
import com.have.blog.core.annotion.avoidRepeatableCommit.AvoidRepeatableCommit;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.BindingResult;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

@Api(value = "博客标签相关接口", tags = {"博客标签相关接口"})
@RestController
@RequestMapping("/tag")
@Slf4j
public class AdminBlogTagController {
    @Autowired
    private AdminBlogTagService tagService;

    @ApiOperation(value = "获取标签列表", notes = "获取标签列表")
    @PostMapping("/getList")
    public Wrapper<CommonPage> getList(@Validated({GetList.class}) @RequestBody BlogTagDto blogTagDto, BindingResult result) {

        // 参数校验
        ThrowableUtils.checkParamArgument(result);
        log.info("获取标签列表");
        List<BlogTagDto> list = tagService.getList(blogTagDto);
        return Wrapper.success(CommonPage.restPage(list));
    }


    @AvoidRepeatableCommit
    @OperationLogger(value = "增加标签")
    @ApiOperation(value = "增加标签", notes = "增加标签")
    @PostMapping("/add")
    public Wrapper add(@Validated({Insert.class}) @RequestBody BlogTagDto blogTagDto, BindingResult result) {

        // 参数校验
        ThrowableUtils.checkParamArgument(result);
        log.info("增加标签");
        return tagService.addTag(blogTagDto);
    }

    @OperationLogger(value = "编辑标签")
    @ApiOperation(value = "编辑标签", notes = "编辑标签")
    @PostMapping("/edit")
    public Wrapper edit(@Validated({Update.class}) @RequestBody BlogTagDto blogTagDto, BindingResult result) {

        // 参数校验
        ThrowableUtils.checkParamArgument(result);
        log.info("编辑标签");
        return tagService.editTag(blogTagDto);
    }

    @OperationLogger(value = "批量删除标签")
    @ApiOperation(value = "批量删除标签", notes = "批量删除标签")
    @PostMapping("/deleteBatch")
    public Wrapper delete(@RequestBody List<Integer> uids) {
        // 参数校验
        log.info("批量删除标签:{}", uids);
        return tagService.deleteBatchTag(uids);
    }

    @OperationLogger(value = "置顶标签")
    @ApiOperation(value = "置顶标签", notes = "置顶标签", response = String.class)
    @PostMapping("/stick")
    public Wrapper stick(@Validated({Delete.class}) @RequestBody BlogTagDto blogTagDto, BindingResult result) {
        // 参数校验
        ThrowableUtils.checkParamArgument(result);
        log.info("置顶标签:{}", blogTagDto);
        return tagService.stickTag(blogTagDto);
    }

    @OperationLogger(value = "通过点击量排序标签")
    @ApiOperation(value = "通过点击量排序标签", notes = "通过点击量排序标签")
    @PostMapping("/tagSortByClickCount")
    public Wrapper tagSortByClickCount() {
        log.info("通过点击量排序标签");
        return tagService.sortTagByClickCount();
    }

    /**
     * 通过引用量排序标签
     * 引用量就是所有的文章中，有多少使用了该标签，如果使用的越多，该标签的引用量越大，那么排名越靠前
     *
     * @return
     */
    @OperationLogger(value = "通过引用量排序标签")
    @ApiOperation(value = "通过引用量排序标签", notes = "通过引用量排序标签", response = String.class)
    @PostMapping("/tagSortByCite")
    public Wrapper tagSortByCite() {
        log.info("通过引用量排序标签");
        return tagService.sortTagByBlogCount();
    }

}
